package com.example.demojpa.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.demojpa.Service.SqlService;
import com.example.demojpa.config.HttpsUtils;
import com.example.demojpa.dao.EnnMap1Dao;
import com.example.demojpa.dao.UserDao;
import com.example.demojpa.domain.EnnMap1;
import com.example.demojpa.domain.GisDto;
import com.example.demojpa.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.transaction.Transactional;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserDao userDao;
    @Autowired
    private EnnMap1Dao ennMap1Dao;
    @Autowired
    private SqlService sqlService;
    @RequestMapping("/findAll")
    public List<User> findAll() {
        return userDao.findAll();
    }

    @RequestMapping("/save")
    public void save() {
        User user1 = new User(null,"vhsj","ksdjklj","sdkljfkl","sdjfklj","skldjl");
        User user2= new User(null,"vhsj","ksdjklj","sdkljfkl","sdjfklj","skldjl");
        User user3 = new User(null,"vhsj","ksdjklj","sdkljfkl","sdjfklj","skldjl");
        User user4= new User(null,"vhsj","ksdjklj","sdkljfkl","sdjfklj","skldjl");

        userDao.save(user1);
    }


    @RequestMapping("/update")
    public void update() throws Exception {
        String url = "https://cmtest.enn.cn/api/ennGisMgr/isWithin?itcode=zuoshumin&iv-user=zuoshumin&t=1729076956000";
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json;charset=UTF-8");
        headers.put("Authorization", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIyOTI0MjYiLCJjaGFubmVsSWQiOjMsInVzZXJJZCI6MjkyNDI2LCJ0aW1lU3RhbXAiOjE3MDgyMTkzNTU4MTd9.h2XmVABz89L-9J6jx_ORbLCKHpUfVOyiBBNP9IbIulk");
        List<EnnMap1> all = ennMap1Dao.findAll();
        for (EnnMap1 ennMap1 : all) {
            GisDto param = new GisDto();
            param.setEcode("50298136");
            param.setSrid(4326);
            param.setX(Double.valueOf(ennMap1.getLng()));
            param.setY(Double.valueOf(ennMap1.getLat()));

            String strMap = JSON.toJSONString(param);
            String post = HttpsUtils.post(url, strMap, headers);
            System.out.println(post);
        }
    }

    @RequestMapping("/update1")
    @Transactional
    public void update1() throws Exception {
        String url = "https://enngis.enn.cn/api/barea/within";
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json;charset=UTF-8");
        List<EnnMap1> all = ennMap1Dao.findAll();
        for (EnnMap1 ennMap1 : all) {
            GisDto param = new GisDto();
            param.setEcode("50298136");
            param.setSrid(4326);
            param.setX(Double.valueOf(ennMap1.getLng()));
            param.setY(Double.valueOf(ennMap1.getLat()));
            String strMap = JSON.toJSONString(param);

            String result = HttpsUtils.post(url, strMap, headers);
            JSONObject jsonObject = JSON.parseObject(result);
            Boolean data = jsonObject.getBoolean("data");


            ennMap1.setRegion(data+"");
            ennMap1Dao.save(ennMap1);
            Thread.sleep(300);
         }
    }


    @RequestMapping("/delete")
    public void delete() {
        sqlService.deleteById(8);
    }



}